package com.kosta.board.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.kosta.board.domain.Board;
import com.kosta.board.service.BoardService;

@Controller
@RequestMapping("board")
public class BoardController {

	private BoardService boardService;
	
	public void setBoardService(BoardService boardService) {
		this.boardService = boardService;
	}

	@RequestMapping("initWrite.do")
	public String initWrite() {
		return "board/write";
	}

	@RequestMapping("saveBoard.do")
	public String saveBoard(Board board, Model model) {
		boardService.saveBoard(board);
		
		List<Board> boards = this.boardService.findBoards();
		model.addAttribute("boards", boards);
		return "board/list";
	}

	@RequestMapping("findBoards.do")
	public String findBoards(Model model) {
		List<Board> boards = boardService.findBoards();

		model.addAttribute("boards", boards);
		return "board/list";
	}
	
	@RequestMapping("findBoard.do")
	public ModelAndView findBoard(@RequestParam int id) {
		ModelAndView mav = new ModelAndView();
		
		Board board = this.boardService.findBoard(id);
		mav.setViewName("board/write");
		mav.addObject("board", board);
		
		return mav;
	}

}
